package com.mxchip.livestarmobile.mobile.state;

import android.content.Context;
import android.text.TextUtils;

import com.mxchip.livestarmobile.mobile.util.Utils;
import com.orhanobut.hawk.Hawk;
import androidx.annotation.NonNull;

import static com.mxchip.livestarmobile.mobile.state.AppConstant.*;


/**
 *
 * 不要用于跨进程使用，虽然也可以用，如果需要数据跨进程请使用OtherDataProvider
 */
public class SPreference {

    /*private static SharedPreferences getBase(@NonNull Context context) {
        return context.getApplicationContext().getSharedPreferences(USER_SETTING, Context.MODE_PRIVATE);
    }*/

    /*private static SharedPreferences getBase(@NonNull String key, @NonNull Context context) {
        return context.getSharedPreferences(key, Context.MODE_PRIVATE);
    }*/

    private static void putString(@NonNull String key, @NonNull String value) {
        Hawk.put(key, value);
        /*SharedPreferences.Editor edit = getBase(context).edit();
        edit.putString(key, value);
        edit.apply();*/
    }

    private static String getString(@NonNull String key) {
        return Hawk.get(key);
        // return getBase(context).getString(key, null);
    }

    private static void putInt(@NonNull String key, @NonNull int value) {
        Hawk.put(key, value);
        /*SharedPreferences.Editor edit = getBase(context).edit();
        edit.putInt(key, value);
        edit.apply();*/
    }

    /**
     * 默认值为-1
     *
     * @param key
     * @return
     */
    private static int getInt(@NonNull String key) {
        return Hawk.get(key);
        // return getBase(context).getInt(key, -1);
    }

    private static void putBoolean(@NonNull String key, @NonNull boolean value) {
        Hawk.put(key, value);
        /*SharedPreferences.Editor edit = getBase(context).edit();
        edit.putBoolean(key, value);
        edit.apply();*/
    }

    private static boolean getBoolean(@NonNull String key) {
        return Hawk.get(key, false);
        // return getBase(context).getBoolean(key, false);
    }

    private static void putLong(@NonNull String key, @NonNull long value) {
        Hawk.put(key, value);

        /*SharedPreferences.Editor edit = getBase(context).edit();
        edit.putLong(key, value);
        edit.apply();*/
    }

    private static long getLong(@NonNull String key) {
        return Hawk.get(key);
        // return getBase(context).getLong(key, 0L);
    }

    private static void putFloat(@NonNull String key, @NonNull float value) {
        Hawk.put(key, value);

        /*SharedPreferences.Editor edit = getBase(context).edit();
        edit.putFloat(key, value);
        edit.apply();*/
    }

    private static float getFloat(@NonNull String key) {
        return Hawk.get(key);
        // return getBase(context).getFloat(key, 0);
    }

    private static <T> void put(@NonNull String key, @NonNull T value) {
        if (value instanceof String) {
            putString(key, (String) value);
        } else if (value instanceof Integer) {
            putInt(key, (Integer) value);
        } else if (value instanceof Boolean) {
            putBoolean(key, (Boolean) value);
        }
        /*else if (value instanceof BaseEntity) {
            try {
                putString(key, new Gson().toJson(((BaseEntity) value).getData()), context);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }*/
    }

    /**
     * 保存新的版本号
     *
     * @param version
     */
    public static void saveNewUpdateVersion(int version) {
        putInt(NEW_UPDATE_VERSION_KEY, version);
    }

    public static int getNewUpdateVersion() {
        return getInt(NEW_UPDATE_VERSION_KEY);
    }

    /**
     * app是否升级了，通过appVersion判断
     *
     * @param isSave 是否自动保存当前版本号
     * @return boolean
     */
    public static boolean isAppUpdate(boolean isSave, @NonNull Context context) {
        boolean b;
        int oldVersion = getInt(LAST_APP_VERSION);
        int nowVersion = Utils.getVersionCode(context);
        if (oldVersion < 0) {
            b = true;
        } else {
            b = oldVersion < nowVersion;
        }
        if (isSave)
            putInt(LAST_APP_VERSION, nowVersion);
        return b;
    }

    /**
     * 保存app是否升级信息，可以用于q
     */
    public static void saveIsAppUpdate(@NonNull Context context) {
        putInt(LAST_APP_VERSION, Utils.getVersionCode(context));
    }

    public static void saveCameraFlash(int which) {
        putInt(CAMERA_FLASH, which);
    }

    public static int getCameraFlash() {
        int which = getInt(CAMERA_FLASH);
        return which < 0 ? 1 : which;
    }

    /**
     * 获得语言设置
     *
     * @return
     */
    public static String getUserLocale() {
        String language = getString(MORE_COUNTRY_LANAGUAGE);
        if (TextUtils.isEmpty(language)) {
            language = "auto";
        }
        return language;
    }

    /**
     * 设置语言
     *
     * @param language
     */
    public static void setUserLocale(String language) {
        putString(MORE_COUNTRY_LANAGUAGE, language);
    }

    public static boolean isSame2SystemLanguage() {
        String oldLan = getString(SYSTEM_LANGUAGE);
        String systemLan = LanguageSettingUtil.getSystemLanguage();
        putString(SYSTEM_LANGUAGE, systemLan);
        return TextUtils.isEmpty(oldLan) || TextUtils.equals(oldLan, systemLan);
    }


    public static Boolean getDoNotDisturbMode() {
        return getBoolean(DO_NOT_DISTURB_MODE);
    }

    public static void setDoNotDisturbMode(Boolean val) {
        putBoolean(DO_NOT_DISTURB_MODE, val);
    }

    public static String getDeviceNumber() {
        return getString(SP_LUXX_NUMBER);
    }

    public static void setDeviceNumber(String val) {
        putString(SP_LUXX_NUMBER, val);
    }

    public static void setDeviceRoomId(String val) {
        putString(SP_LUXX_ROOM_ID, val);
    }

    public static String getDeviceRoomId() {
        return getString(SP_LUXX_ROOM_ID);
    }

    public static void setDeviceToken(String val) {
        putString(SP_LUXX_TOKEN, val);
    }

    public static String getDeviceToken() {
        return getString(SP_LUXX_TOKEN);
    }


}
